//
//  XHPhotoViewController.m
//  elxc
//
//  Created by 冷山 on 16/3/14.
//  Copyright © 2016年 lexingsoft. All rights reserved.
//

#import "XHPhotoViewController.h"


#import "Services.h"
#import "UIActionSheet+Blocks.h"
#import "ImagePickerAction.h"
#import <SDWebImage/UIImageView+WebCache.h>

#define SCREEN_HEIGHT                   ([UIScreen mainScreen].bounds.size.height)
#define SCREEN_WIDTH                    ([UIScreen mainScreen].bounds.size.width)

@interface XHPhotoViewController ()
@property (strong, nonatomic) ImagePickerAction *imagePickerAction;
@property (weak, nonatomic) IBOutlet NSLayoutConstraint *la_cancelHeight;
@property (weak, nonatomic) IBOutlet NSLayoutConstraint *la_viewHeight;
@property (weak, nonatomic) IBOutlet UIView *ui_cancelView;
@property (weak, nonatomic) IBOutlet UIView *ui_photoAlbumView;
@property (weak, nonatomic) IBOutlet UIView *ui_photoView;

@property (strong, nonatomic) UIView * bgView;
@property (strong, nonatomic) UIButton *photoAlbumButton;
@property (strong, nonatomic) UIButton *photoButton;

@property(nonatomic,strong)NSTimer * timer;

@end

@implementation XHPhotoViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    
    self.ui_photoView.hidden = YES;
    self.ui_cancelView.hidden = YES;
    self.ui_photoAlbumView.hidden = YES;
}
- (void)viewWillAppear:(BOOL)animated
{
    [super viewWillAppear:animated];
//    [self animation];
    [self drawView];
}

- (void)viewWillDisappear:(BOOL)animated{
    [super viewWillDisappear:animated];
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
}

//绘制曲线
- (void)drawView
{
    CGPoint startPoint = CGPointMake(SCREEN_WIDTH/2,SCREEN_HEIGHT);
    CGPoint endPoint = CGPointMake(SCREEN_WIDTH/2,SCREEN_HEIGHT/2);
    
    CABasicAnimation *positionAnimation = [CABasicAnimation animationWithKeyPath:@"position"];
    positionAnimation.duration=.3;
    positionAnimation.timingFunction = [CAMediaTimingFunction functionWithName:kCAMediaTimingFunctionEaseInEaseOut];
    positionAnimation.fromValue = [NSValue valueWithCGPoint:startPoint];
    positionAnimation.toValue = [NSValue valueWithCGPoint:endPoint];
    positionAnimation.beginTime = CACurrentMediaTime() + (0.3/9);
    positionAnimation.fillMode = kCAFillModeForwards;
    positionAnimation.removedOnCompletion = NO;
    
    [self.view.layer addAnimation:positionAnimation forKey:@"positionAnimation"];
    self.view.layer.position=endPoint;
    
    CABasicAnimation *scaleAnimation = [CABasicAnimation animationWithKeyPath:@"transform.scale"];
    scaleAnimation.duration=.3;
    scaleAnimation.timingFunction = [CAMediaTimingFunction functionWithName:kCAMediaTimingFunctionEaseInEaseOut];
    scaleAnimation.fromValue = @(0);
    scaleAnimation.toValue = @(1);
    scaleAnimation.beginTime = CACurrentMediaTime() + (0.3/9);
    scaleAnimation.fillMode = kCAFillModeForwards;
    scaleAnimation.removedOnCompletion = NO;
    
    self.bgView = [[UIView alloc]initWithFrame:CGRectMake(0, 0, SCREEN_WIDTH,SCREEN_HEIGHT)];
    self.bgView.userInteractionEnabled = YES;
    [self.view addSubview:self.bgView];
    
    CGPoint point1 = CGPointMake(0, SCREEN_HEIGHT-200);
    CGPoint point2 = CGPointMake(0, SCREEN_HEIGHT);
    CGPoint point3 = CGPointMake(SCREEN_WIDTH, SCREEN_HEIGHT);
    CGPoint point4 = CGPointMake(SCREEN_WIDTH, SCREEN_HEIGHT-200);
    
    UIBezierPath * path = [UIBezierPath bezierPath];
    [path moveToPoint:point1];
    [path addLineToPoint:point2];
    [path addLineToPoint:point3];
    [path addLineToPoint:point4];
    [path addQuadCurveToPoint:point1 controlPoint:CGPointMake(SCREEN_WIDTH/2,SCREEN_HEIGHT-(SCREEN_WIDTH*50.0f/375+200))];
    
    CAShapeLayer * shapeLayer = [CAShapeLayer layer];
    shapeLayer.path = path.CGPath;
    shapeLayer.fillColor = [UIColor whiteColor].CGColor;
    [self.bgView.layer addSublayer:shapeLayer];

    
    [self.bgView.layer addAnimation:scaleAnimation forKey:@"transformscale"];
    self.bgView.transform = CGAffineTransformMakeScale(0.01f, 0.01f);
    
    
    [self createUI];
}

- (void)createUI
{
    UIButton * cancel = [[UIButton alloc]init];
    cancel.frame = CGRectMake(SCREEN_WIDTH/2-8,SCREEN_HEIGHT-44,16,22);
    [cancel setImage:[UIImage imageNamed:@"geren_icon_guanbi"] forState:UIControlStateNormal];
    [cancel addTarget:self action:@selector(dissMissViewControllers) forControlEvents:UIControlEventTouchUpInside];
    [self.view addSubview:cancel];
    
    UIButton * photo = [[UIButton alloc]init];
    photo.frame = CGRectMake(SCREEN_WIDTH/4-30, SCREEN_HEIGHT-150, 60, 60);
    [photo setImage:[UIImage imageNamed:@"geren_icon_paizhao"] forState:UIControlStateNormal];
    [photo addTarget:self action:@selector(photoAction:) forControlEvents:UIControlEventTouchUpInside];
    [self.view addSubview:photo];
    
    UILabel * photoLabel = [[UILabel alloc]init];
    photoLabel.font = [UIFont systemFontOfSize:15];
    photoLabel.frame = CGRectMake(SCREEN_WIDTH/4-15,SCREEN_HEIGHT-80,30,20);
    photoLabel.text = @"照相";
    photoLabel.textColor = [ColorManager colorWithHexString:@"#000000" alpha:0.5];
    [self.view addSubview:photoLabel];
    
    UIButton * photoAlbum = [[UIButton alloc]init];
    photoAlbum.frame = CGRectMake(SCREEN_WIDTH*3/4-30, SCREEN_HEIGHT-150, 60, 60);
    [photoAlbum setImage:[UIImage imageNamed:@"geren_icon_xiangce"] forState:UIControlStateNormal];
    [photoAlbum addTarget:self action:@selector(photoAlbumAction:) forControlEvents:UIControlEventTouchUpInside];
    [self.view addSubview:photoAlbum];
    
    UILabel * photoAlbumLabel = [[UILabel alloc]init];
    photoAlbumLabel.font = [UIFont systemFontOfSize:15];
    photoAlbumLabel.frame = CGRectMake(SCREEN_WIDTH*3/4-15,SCREEN_HEIGHT-80,30,20);
    photoAlbumLabel.text = @"相册";
    photoAlbumLabel.textColor = [ColorManager colorWithHexString:@"#000000" alpha:0.5];
    [self.view addSubview:photoAlbumLabel];
}

- (void)animation
{
    CGPoint startPoint = CGPointMake(0, 194);
    CGPoint startPoint1 = CGPointMake(SCREEN_WIDTH/2, 194);
    self.ui_cancelView.layer.position=startPoint;
    self.ui_photoAlbumView.layer.position=startPoint;
    self.ui_photoView.layer.position=startPoint1;
    //damping:阻尼，范围0-1，阻尼越接近于0，弹性效果越明显
    //velocity:弹性复位的速度
    [UIView animateWithDuration:.8 delay:0 usingSpringWithDamping:.5 initialSpringVelocity:1.0 options:UIViewAnimationOptionCurveLinear animations:^{
        CGPoint startPoint = CGPointMake(0, -194);
        CGPoint startPoint1 = CGPointMake(SCREEN_WIDTH/2, -194);
        self.la_cancelHeight.constant = 0;
        self.ui_cancelView.layer.position=startPoint;
        self.ui_photoAlbumView.layer.position =startPoint;
        self.ui_photoView.layer.position=startPoint1;
        
        self.la_viewHeight.constant = 0;
        
    } completion:^(BOOL finished) {
        
    }];
}


//照相
- (IBAction)photoAction:(id)sender
{
    if (!self.imagePickerAction)
    {
        self.imagePickerAction = [[ImagePickerAction alloc] initWithShowViewController:self];
    }
    WS(weakSelf)
    [self.imagePickerAction ownShowImageHeaderPickWithCameraBlock:^(NSArray *imageArray) {
        [[UIApplication sharedApplication] setStatusBarHidden:NO];
        [[UIApplication sharedApplication] setStatusBarStyle:UIStatusBarStyleLightContent animated:NO];
        
        UIImage * pickerImage = (UIImage*)imageArray[0];
        [PromptAction showWaitPrompt:@"上传中..."];
        [weakSelf.tabBarController.view setUserInteractionEnabled:NO];
        
        [ServiceManager updateHeaderImage:pickerImage success:^(NSDictionary *data) {
            [PromptAction showSuccessPrompt:@"上传成功"];
            [self dismissViewControllerAnimated:NO completion:^{
                [[NSNotificationCenter defaultCenter] postNotificationName:@"reloadData" object:nil];
            }];
            [weakSelf.tabBarController.view setUserInteractionEnabled:YES];
        } failure:^{
            [weakSelf.tabBarController.view setUserInteractionEnabled:YES];
        }];
    }];

}
//相册
- (IBAction)photoAlbumAction:(id)sender
{
    if (!self.imagePickerAction)
    {
        self.imagePickerAction = [[ImagePickerAction alloc] initWithShowViewController:self];
    }
    WS(weakSelf)
    [self.imagePickerAction ownShowImageHeaderPickWithPhotoLibraryBlock:^(NSArray *imageArray) {
        [[UIApplication sharedApplication] setStatusBarHidden:NO];
        [[UIApplication sharedApplication] setStatusBarStyle:UIStatusBarStyleLightContent animated:NO];
        
        UIImage * pickerImage = (UIImage*)imageArray[0];
        [PromptAction showWaitPrompt:@"上传中..."];
        [weakSelf.tabBarController.view setUserInteractionEnabled:NO];
        [ServiceManager updateHeaderImage:pickerImage success:^(NSDictionary *data) {
            [PromptAction showSuccessPrompt:@"上传成功"];
            [self dismissViewControllerAnimated:NO completion:^{
                [[NSNotificationCenter defaultCenter] postNotificationName:@"reloadData" object:nil];
            }];
            [weakSelf.tabBarController.view setUserInteractionEnabled:YES];
        } failure:^{
            [weakSelf.tabBarController.view setUserInteractionEnabled:YES];
        }];
    }];

}
//取消
- (IBAction)cancelAction:(id)sender
{
    [UIView animateWithDuration:0.35 animations:^{
        
        self.la_cancelHeight.constant = -194;
        [self.view layoutIfNeeded];
    }];
    self.timer = [NSTimer scheduledTimerWithTimeInterval:0.35
                                                  target:self selector:@selector(dissMissViewController) userInfo:nil repeats:YES];
}
- (void)dissMissViewControllers
{
    [self dismissViewControllerAnimated:NO completion:^{
        
    }];
}


/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
